﻿using Direct.IDAL.Manager.Notice;
using DirectSell.Common.Url;
using DirectSell.Model.ManagerNotice;
using DirectSell.Model.Query;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.Manager.Notice
{
    /// <summary>
    /// 发布公告仓储层
    /// </summary>
   public class NoticeManagerRepository:BaseManagerRepository, INoticeManagerRepository
    {
        #region 发布公告
        /// <summary>
        /// 发布公告
        /// </summary>
        public bool NoticeAdd(NoticeReq req)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" INSERT INTO Notice ");
            sb.Append(" ([Title] ");
            sb.Append(" ,[Author] ");
            sb.Append(" ,[CreateDate] ");
            sb.Append(" ,[Sort] ");
            sb.Append(" ,[Ncontent] ");
            sb.Append("  ,[State]) ");
            sb.Append(" VALUES ");
            sb.AppendFormat(" ('{0}' ",req.Title);
            sb.AppendFormat(" ,'{0}' ",req.Author);
            sb.AppendFormat(" ,'{0}' ",req.CreateDate);
            sb.AppendFormat(" ,{0} ",req.Sort);
            sb.AppendFormat(" ,'{0}' ",req.Ncontent);
            sb.AppendFormat(" ,0) ");
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 查询公告列表
        /// <summary>
        /// 查询公告列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<PagedList<Notices>> GetList(BaseCondition condition) 
        {
            PageParam model = new PageParam();
            model.OrderBy = "Sort ASC";
            model.PageIndex = condition.pageIndex == 0 ? 1 : condition.pageIndex;
            model.PageSize =condition.pageSize;
            model.Fields = "*";
            model.TableName = "Notice";
            StringBuilder sb = new StringBuilder();
            if (condition.SerachCondition != null)
            {
                sb.AppendFormat(" (Title LIKE '%{0}%' OR Ncontent  LIKE '%{0}%' ) ", condition.SerachCondition.Trim());
            }

            if (!string.IsNullOrEmpty(condition.startTime))
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND CreateDate>='{0}' and CreateDate<='{1}' ", condition.startTime, condition.endTime);
                }
                else
                {
                    sb.AppendFormat(" AND CreateDate>='{0}' and CreateDate<='{1}' ", condition.startTime, DateTime.Now.ToString());
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND CreateDate>='2010-01-01' and CreateDate<='{1}' ", condition.endTime);
                }
            }
            model.StrWhere = sb.ToString();
            var result = await base.PageQuery<Notices>(model);
            return result;
            //StringBuilder sb = new StringBuilder();
            //sb.Append(" SELECT * FROM Notice WHERE 1=1 ");
            //if (condition.SerachCondition != null)
            //{
            //    sb.AppendFormat("AND (Title LIKE '%{0}%' OR Ncontent  LIKE '%{0}%' ) ", condition.SerachCondition.Trim());
            //}

            //if (!string.IsNullOrEmpty(condition.startTime))
            //{
            //    if (!string.IsNullOrEmpty(condition.endTime))
            //    {
            //        sb.AppendFormat(" AND CreateDate>='{0}' and CreateDate<='{1}' ", condition.startTime, condition.endTime);
            //    }
            //    else
            //    {
            //        sb.AppendFormat(" AND CreateDate>='{0}' and CreateDate<='{1}' ", condition.startTime, DateTime.Now.ToString());
            //    }
            //}
            //else
            //{
            //    if (!string.IsNullOrEmpty(condition.endTime))
            //    {
            //        sb.AppendFormat(" AND CreateDate>='2010-01-01' and CreateDate<='{1}' ", condition.endTime);
            //    }
            //}
            //sb.Append(" ORDER BY Sort ASC");
            //var result = await base.QueryAsync<Notices>(sb.ToString());
            //return result as IList<Notices>;
        }
        #endregion

        #region 查一条公告
        /// <summary>
        /// 查一条公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Notices QueryNotice(int id)
        {
            string str = string.Format(" SELECT * FROM Notice WHERE Id={0} ", id);
            return base.Query<Notices>(str).FirstOrDefault();
        }
        #endregion

        #region 删除公告
        /// <summary>
        /// 删除公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelNotice(int id)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("DELETE FROM Notice WHERE Id = {0}", id);
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 修改一条公告
        /// <summary>
        /// 修改一条公告
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool UpdateNotice(NoticeReq req)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE Notice SET ");
            sb.AppendFormat(" Title='{0}' , ", req.Title);
            sb.AppendFormat(" Author='{0}' , ", req.Author);
            sb.AppendFormat(" Sort={0} , ", req.Sort);
            sb.AppendFormat(" Ncontent='{0}' ", req.Ncontent);
            sb.AppendFormat(" WHERE Id= {0} ", req.Id);
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 修改排序
        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public bool UpdateSort(int id,int sort)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE Notice SET ");
            sb.AppendFormat(" Sort={0}  ", sort);
            sb.AppendFormat(" WHERE Id= {0} ", id);
            return base.Execute(sb.ToString());
        }
        #endregion

    }
}
